﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;


namespace BaseAttack
{
    public class Factory
    {
        private Canvas UI;
        private Rectangle Base;
        private Point Position;
        private TextBlock CashText;
        private int Cash;
        private Point NewUnitVector;

        public Factory(Canvas UI, Point Position, int Orientation, int AvailableCash)
        {
            this.Position = Position;
            this.UI = UI;
            this.Cash = AvailableCash;

            Base = new Rectangle();
            Base.Fill = Brushes.Purple;
            Base.Width = 50;
            Base.Height = 20;
            Base.LayoutTransform = new RotateTransform(90 * Orientation);
            UI.Children.Add(Base);
            Canvas.SetTop(Base, Position.Y - (Base.Height / 2));
            Canvas.SetLeft(Base, Position.X - (Base.Width / 2));

            CashText = new TextBlock();
            CashText.Foreground = Brushes.White;
            CashText.Text = "$" + AvailableCash;
            CashText.Visibility = Visibility.Visible;
            CashText.LayoutTransform = new RotateTransform(90 * Orientation);
            UI.Children.Add(CashText);
            Canvas.SetTop(CashText, Position.Y - (Base.Height / 2));
            Canvas.SetLeft(CashText, Position.X - (Base.Width / 2));

            switch (Orientation)
            {
                case 0:
                    NewUnitVector = new Point(1, 0);
                    break;
                case 1:
                    NewUnitVector = new Point(0, 1);
                    break;
                case 2:
                    NewUnitVector = new Point(-1, 0);
                    break;
                case 3:
                    NewUnitVector = new Point(0, -1);
                    break;
            }
        }

        public Unit BuildUnit(List<Unit> Units, ref int NewCash)
        {
            if (Cash > 150)
            {
                NewCash = Cash - 150;

                Point p = Position;
                /*
                do
                {
                    p.X += 55 * NewUnitVector.X;
                    p.Y += 55 * NewUnitVector.Y;
                } while (U.GetUnitAt(p, Units) != null);
                 */
                p.X += 50 * NewUnitVector.X;
                p.Y += 50 * NewUnitVector.Y;
                Unit u = new Unit(UI, new Point(p.X, p.Y));
                Units.Add(u);
                return u;
            }

            return null;
        }

        public Boolean HitTest(Point p)
        {
            return (U.Distance(Position, p) < 20);
        }

        public Point Center
        {
            get { return Position; }
        }

        public void UpdateAvailableCash(int Cash)
        {
            this.Cash = Cash;
            this.CashText.Text = "$" + Cash.ToString();
            if (Cash < 150)
            {
                this.CashText.Foreground = Brushes.Gray;
            }
            else
            {
                this.CashText.Foreground = Brushes.White;
            }
        }

        public Canvas getUI(){
            return this.UI;
        }
        public Point getPosition()
        {
            return this.Position;
        }
        public double getNewUnitVectorX()
        {
            return this.NewUnitVector.X;
        }
        public double getNewUnitVectorY()
        {
            return this.NewUnitVector.Y;
        }
        public int getCash()
        {
            return this.Cash;
        }
        public void setCash(int c)
        {
            this.Cash = c;
        }
    }
}
